package com.taokatao.cardmee.business.profile;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.taokatao.cardmee.App;
import com.taokatao.cardmee.BaseActivity;
import com.taokatao.cardmee.C;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.api.ApiManager;
import com.taokatao.cardmee.api.RespWrapper;
import com.taokatao.cardmee.api.SubscriberAdapter;
import com.taokatao.cardmee.api.model.SecretCardInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class ProfileReceiveActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.e_input) EditText eInput;
    @BindView(R.id.t_error) TextView tError;
    @BindView(R.id.t_submit) TextView tSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_receive);
        ButterKnife.bind(this);

        eInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                tSubmit.setEnabled(!TextUtils.isEmpty(eInput.getText().toString()));
            }
        });
        tSubmit.setEnabled(false);
    }

    @Override
    @OnClick({R.id.t_submit})
    public void onClick(View v) {
        if (v == tSubmit) {
            tError.setVisibility(View.INVISIBLE);
            String inputContent = eInput.getText().toString();
            requestSecretToAddCard(inputContent);
        }
    }

    private void requestSecretToAddCard(String secret) {
        ApiManager.i().secretToAddCard(this, App.i().getCurrUser().getUuid(), secret, new SubscriberAdapter<RespWrapper<SecretCardInfo>>() {
            @Override
            public void onNext(RespWrapper<SecretCardInfo> o) {
                super.onNext(o);
                if (o.code == C.http.RESULT_CODE_SUCCESS) {
                    Intent intent = new Intent(ProfileReceiveActivity.this, ProfileReceiveInfoActivity.class);
                    intent.putExtra("extra_secret_info", o.decode(SecretCardInfo.class));
                    startActivity(intent);
                } else {
                    tError.setVisibility(View.VISIBLE);
                }
            }
        }.errorToast(true).showProgress(this));
    }
}
